CROSS_COMPILE 	= arm-linux-gnueabihf-
AS 				= $(CROSS_COMPILE)as 
LD				= $(CROSS_COMPILE)ld 
CC				= $(CROSS_COMPILE)gcc
STRIP			= $(CROSS_COMPILE)strip
OBJCOPY			= $(CROSS_COMPILE)objcopy 
OBJDUMP 		= $(CROSS_COMPILE)objdump 

arch			:= arm
LINUXINCLUDE	:= -I./include -I./include/linux -I./arch -I./arch/arm  -I./arch/arm/include -I./arch/arm/mach-imx -I./arch/arm/mach-imx/include
KBUILD_AFLAGS	:= -D__ASSEMBLY__ -Wall -g -marm -fno-builtin -Wno-unused-const-variable -Wno-unused-function  -nostdlib 
KBUILD_CFLAGS   := -Wall -g -marm -fno-builtin -Wno-unused-const-variable -Wno-unused-function -nostdlib 
LDFLAGS         := -Tyap.lds

# Usage:
#  $(call cmd, cc_o_c)              
quiet_cmd_cc_o_c = CC $(subst ../, ,$<)
cmd_cc_o_c 		 = $(CC) $(KBUILD_CFLAGS) $(LINUXINCLUDE)  -c $< -o $@ 
cmd 			 = @echo  "$(quiet_cmd_cc_o_c)"; $(cmd_cc_o_c)

quiet_cmd_cc_o_asm 	= CC $(subst ../, ,$<)
cmd_cc_o_asm 	 	= $(CC) $(KBUILD_AFLAGS) $(LINUXINCLUDE)  -c $< -o $@ 
cmd_asm				= @echo  "$(quiet_cmd_cc_o_asm)"; $(cmd_cc_o_asm)


obj-y			=
TARGET 	 		= yap.elf

TOP_DIR  		= .
FS_DIR  		= $(TOP_DIR)/fs
MM_DIR  		= $(TOP_DIR)/mm
IPC_DIR 		= $(TOP_DIR)/ipc
LIB_DIR 		= $(TOP_DIR)/lib
NET_DIR 		= $(TOP_DIR)/net
ARCH_DIR 		= $(TOP_DIR)/arch
PROC_DIR 		= $(TOP_DIR)/proc
DRIVER_DIR 		= $(TOP_DIR)/drivers
KERNEL_DIR 		= $(TOP_DIR)/kernel
SCRIPTS_DIR 	= $(TOP_DIR)/scripts
INIT_DIR 		= $(TOP_DIR)/init
USER_DIR 		= $(TOP_DIR)/user
TEST_DIR		= $(TOP_DIR)/test

include $(ARCH_DIR)/Makefile
include $(KERNEL_DIR)/Makefile
include $(FS_DIR)/Makefile
include $(LIB_DIR)/Makefile
include $(MM_DIR)/Makefile
include $(NET_DIR)/Makefile
include $(DRIVER_DIR)/Makefile
include $(SCRIPTS_DIR)/Makefile
include $(INIT_DIR)/Makefile
include $(USER_DIR)/Makefile

DEPS = $(obj-y:.o=.d)

all: $(obj-y) $(DEPS)	
	@echo LD $(TARGET)
	@$(LD) $(LDFLAGS) -g -o $(TARGET) $(obj-y)
	make -C debug

#@echo OBJCOPY $(TARGET).bin
#@$(OBJCOPY) -O binary $(TARGET) $(TARGET).bin
	
#@echo OBJDUMP $(TARGET).dis
#@$(OBJDUMP) -D $(TARGET) > $(TARGET).dis 

-include $(DEPS)

%.o: %.c
	$(call cmd,cc_o_c)

%.o: %.s
	$(call cmd_asm,cc_o_asm)

%.o: %.S
	$(call cmd_asm,cc_o__asm)

%.d: %.c
	@$(CC) -MM -E $(KBUILD_CFLAGS) $(LINUXINCLUDE) $< | sed 's,\(.*\)\.o[ :]*, $(<:.c=.o) $@: ,g' > $@

%.d: %.s
	@echo   > $@

%.d: %.S
	@echo   > $@

clean:
	@echo rm *.o $(TARGET) *.bin *.dis *.d -rf
	@rm $(TARGET) $(obj-y) *.bin *.dis $(DEPS) -rf
	make -C debug clean

